{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Measurement class basics\n",
    "Objects of the **`measurement class`** are used to save all the information for one single measurement (in contrast to an object of the **`sample`** class that is used for multiple measurements).<br>\n",
    "A `measurement` object is currently created for spectroscopy and transport measurements, with time domain measurement support coming in the near future. Besides **all used instrument settings** it records<br>\n",
    "* `UUID`, `run`, and `user`\n",
    "* measurement type (i.e. `spectroscopy`), used function (i.e. `measure_2D()`), and measurement axis\n",
    "* used `sample` object (in its then form)\n",
    "* git commit id of `qkit`\n",
    "\n",
    "The measurement object is saved in the hdf5 file, viewable with `qviewkit` and saved in a separate `.measurement` file in the same folder as the hdf5 file. A saved measurement object can be loaded similar to a data file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## start qkit and import the necessary classes; here we assume a already configured qkit environment \n",
    "import qkit\n",
    "qkit.start()\n",
    "\n",
    "from qkit.measure.measurement_class import Measurement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In `qkit`'s **file information database** there is a dictionary, mapping the **UUID** of the data file to the absolute **path of the saved `.measurement` file**. This can be loaded by creating a **`measurement` object and parsing the abspath**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = Measurement(qkit.fid.measure_db['XXXXXX'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The information in the file is **JSON encoded** (basically a **large dict**) and upon init gets **converted into object attributes**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user = m.user\n",
    "run_id = m.run_id\n",
    "smpl = m.sample"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Besides this readout of parameters is also possible to **set an instrument back** to its settings during this measurement to **recreate the mesurement** environment. For this to work the specific **instruments need to be initialized first**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.update_instrument('vna')\n",
    "\n",
    "## also possible for all measurements\n",
    "## m.update_all_instruments()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The entries can be **added or changed** (be careful not to lose information) **and saved again**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.analyzed = True\n",
    "m.rating = 5\n",
    "m.save(qkit.fid.measure_db['XXXXXX'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
